package gdconf

import (
	"server_cluster/common/logger"
)

// 家园建筑配置表

type HomeBuildingConfig struct {
	BuildingId         int32         `csv:"Id" gorm:"primary_key;autoIncrement:false"` // 建筑id
	Type               int32         `csv:"Type,omitempty"`                            // 建筑类型
	BuildingLevelGroup int32         `csv:"BuildGroup,omitempty"`                      // 建筑等级组
	BuildingNumLimit   IntArrayArray `csv:"LimitNum,omitempty"`                        // 家园等级建筑数量限制
}

func (h *HomeBuildingConfig) TableName() string {
	return "config_home_building"
}

func (g *GameDataConfig) saveHomeBuildingConfig() {
	saveTableToDb[HomeBuildingConfig](g.Db, readTable[HomeBuildingConfig](g.CsvPathPrefix+"HomeBuild.csv"))
}

func (g *GameDataConfig) loadHomeBuildingConfig() {
	g.GameDataMaps.HomeBuildingConfigMap = make(map[int32]*HomeBuildingConfig)
	for _, homeBuildingConfig := range loadTableFromDb[HomeBuildingConfig](g.Db) {
		g.GameDataMaps.HomeBuildingConfigMap[homeBuildingConfig.BuildingId] = homeBuildingConfig
	}
	logger.Info("HomeBuildingConfig Count: %v", len(g.GameDataMaps.HomeBuildingConfigMap))
}

func GetHomeBuildingConfig(buildingId int32) *HomeBuildingConfig {
	value, exist := CONF.GameDataMaps.HomeBuildingConfigMap[buildingId]
	if !exist {
		logger.Error("[ConfigNotFound] HomeBuildingConfig, buildingId: %v", buildingId)
		return nil
	}
	return value
}

func GetHomeBuildingConfigMap() map[int32]*HomeBuildingConfig {
	return CONF.GameDataMaps.HomeBuildingConfigMap
}
